<?php
/**
 * 产品促销管理
 * @author cjli
 *
 */
class CouponAction extends AdminAction
{
	public function index()
	{
		$this->wholesale_list();
	}
	public function coupon_list(){
		$coupon_db = D("favorable");
        $where = '(status =1 OR status = 4) and favorable_group is null and coupon_code not like "subs%" and coupon_code not like "es%"  ';
		//分页
		$count = $coupon_db->where($where)->count();
		import("ORG.Util.Page");
		$Page = new Page($count,20);
		$pageShow = $Page->show();
		$this->assign('pageShow', $pageShow);

		//$ss = $coupon_db->field('id,coupon_code,coupon_money');
		$coupon_db->order('id DESC');
		$coupon_db->limit($Page->firstRow.','.$Page->listRows);
		$coupon_db->where($where);
		$couponList = $coupon_db->select();
        //echo $coupon_db->getLastSql();die;
		//计算优惠码使用数量
		$orders_db = D("orders");
		$newlist=array();
		foreach($couponList as $key=>$val){
   
    		$newlist[$key]['count']=$orders_db->where(array('coupon_code'=>$val['coupon_code'],'payment_status'=>1))->count();
   
		}	
        $group_coupon = $coupon_db->field('*,sum(usage_count) as use_count,sum(count) as count')->where('favorable_group is not null')->group('favorable_group')->select();
        if($group_coupon){
            $this->assign('group_coupon',$group_coupon);
        }
		$this->assign('newlist',$newlist);
		$this->assign('couponList',$couponList);

		$this->display('coupon_list');
	}
	public function coupon_edit(){

		$coupon_db = D('favorable');
		$coupon_id = intval($_GET['coupon_id']);
		if($_POST['dosubmit']){
			$checkdata['coupon_code'] = trim($_POST['coupon_code']);
			if($coupon_id){
				$checkdata['id'] = array('neq', $coupon_id);
			}
			$count = $coupon_db->where($checkdata)->count();

			//检测是否存在相同名称的红包
			if($count){
				$this->error('优惠码名称存在，请使用其他名称', U('Coupon/coupon_list', array('coupon_id' => $coupon_id)));
			}

			$coupon_id = PromotionModel::updateCouponInfo($_POST, $coupon_id);
			if($coupon_id){
				$this->success('优惠码编辑成功！', U('Coupon/coupon_list'));
			}else{
				$this->error('优惠码类型编辑失败！', U('Coupon/coupon_list'));
			}

		}else{
			$couponInfo = array();
			if($coupon_id){
				$couponInfo = $coupon_db->find($coupon_id);
			}
			$this->assign('bonus', $couponInfo);
			//$this->display('coupon_form');
		}
		$this->display('coupon_form');
	}
    //优惠码组
    public function coupon_edit_special(){

		$coupon_db = D('favorable');
		$group_name = $_GET['group_name'];
		if($_POST['dosubmit']){
            if(!$group_name){
			$coupon_id = PromotionModel::addGroupCouponInfo($_POST);
            }else{
			$coupon_id = PromotionModel::addGroupCouponInfo($_POST,$group_name);
            }
			if($coupon_id){
				$this->success('优惠码编辑成功！', U('Coupon/coupon_list'));
			}else{
				$this->error('优惠码类型编辑失败！', U('Coupon/coupon_list'));
			}

		}else{
			$couponInfo = array();
			if($group_name){
                $where['favorable_group'] = $group_name;
				$couponInfo = $coupon_db->where($where)->order('id')->select();
                foreach($couponInfo as $k=>$v){
                    $codes .= $v['coupon_code'].',';
                }
               $couponInfo[0]['codes'] = trim($codes,',');
            $this->assign('count',count($couponInfo));
			$this->assign('bonus', $couponInfo[0]);
			}

			//$this->display('coupon_form');
		}
        $this->display('coupon_form_special');
    }
    //删除优惠码组
    public function coupon_delete_special(){
        $group_name = $_GET['group_name'];
        $coupon_db = D('favorable');
        $coupon_db->where('favorable_group="'.$group_name.'"')->delete();
       $this->success('优惠码组删除成功！', U('coupon/coupon_list'));
    }
		//删除优惠码类型
	public function coupon_delete()
	{
		$bid = intval($_GET['coupon_id']);
		$coupon_db = D('favorable');
		$coupon_info = $coupon_db->where('id='.$bid)->find();
		if(! $coupon_info){
			$this->error('优惠码不存在！', U('coupon/coupon_list'));
		}
		$coupon_db->delete($bid);
		S('coupon_list', NULL);
		$this->success('优惠码删除成功！', U('coupon/coupon_list'));
	}
    
    public function create_codes(){

        $num = intval($_GET['codes_num']);
        $prefix = trim($_GET['prefix']);
        $len = strlen($prefix);
       
        $coupon_code = '';
        for($i=1;$i<=$num;$i++){
            if($len){
                $str = $prefix.substr(md5(time() . mt_rand(1,1000000)),0,9-$len);
            }else{
                $str = date('d',time()).substr(md5(time() . mt_rand(1,1000000)),0,6);
            }
            $coupon_code .= $str.',';
        }
        echo trim($coupon_code,',');
    }

}